package com.xiaoq.feature.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.feature.entity.CommentEntity;
import com.xiaoq.feature.repository.ICommentRepository;
import com.xiaoq.vo.PagingVo;

@Repository
public class CommentRepositoryImpl implements ICommentRepository {

	@Autowired
	private Datastore datasotre;
	
	private static final Logger LOG = LoggerFactory.getLogger(CommentRepositoryImpl.class);
	
	@Override
	public List<CommentEntity> findCommentList(String shopId, String tag, PagingVo pvo) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Query<CommentEntity> commentQuery = datasotre.find(CommentEntity.class);
		commentQuery.field("shopId").equal(shopId);
		if(StringUtils.isNotEmpty(tag)){
			commentQuery.field("content").contains(tag);
		}
		Long total = 0L;
		List<CommentEntity> commentList = new ArrayList<>();
		total = commentQuery.count();
		commentList = commentQuery.offset(pn).limit(ps).asList();
		pvo.setTc(total);
		return commentList;
	}

	@Override
	public List<CommentEntity> findCommentLists(String shopId) {
		Query<CommentEntity> commentQuery = datasotre.find(CommentEntity.class);
		commentQuery.field("shopId").equal(shopId);
		return commentQuery.asList();
	}

	@Override
	public boolean addComment(CommentEntity comment) {
		datasotre.save(comment);
		return true;
	}

	@Override
	public long findCommentNums(String shopId) {
		Long total = 0L;
		Query<CommentEntity> commentQuery = datasotre.find(CommentEntity.class);
		commentQuery.field("shopId").equal(shopId);
		total = commentQuery.count();
		return total;
	}

}
